#include "gwdetector.h"

#include <QPainter>
#include <qgraphicsscene.h>
#include <QDebug>

GwDetector::GwDetector()
{
    this->createPropertyList();
    this->image.load(":/icons/GW-ok.png");
    if(this->movie==NULL)
    {
        this->movie=new QTimeLine();
    }
    this->movie->setDuration(1500);
    this->movie->setFrameRange(0,this->range);
    movie->setLoopCount(0);
    connect(movie,&QTimeLine::frameChanged,this,&GwDetector::movieRun);
    this->setFlags(ItemIsSelectable);
}

GwDetector::GwDetector(const GwDetector &other)
{
    this->createPropertyList();
    this->image=other.image;
    this->chID=other.chID;
    this->setPos(other.pos());
    if(this->movie==NULL)
    {
        this->movie=new QTimeLine();
    }
    movie=new QTimeLine(500);
    movie->setFrameRange(0,range);
    movie->setLoopCount(0);
    connect(movie,&QTimeLine::frameChanged,this,&GwDetector::movieRun);
    this->setFlags(ItemIsSelectable);
}

GwDetector::~GwDetector()
{
    this->movie->stop();
    delete this->movie;
    delete this->m_pVarFactory;
    delete this->m_pVarManager;
    delete this->m_stringFactory;
    delete this->m_stringManager;
}


QRectF GwDetector::boundingRect() const
{
    return QRectF(0,0,40,40);
//    return QRectF(-this->radius+20,-this->radius+20,this->radius*2,this->radius*2);
}

void GwDetector::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
//    QColor color(Qt::yellow);
    qreal drad=0;//rad=40,
    QPen pen;
    painter->drawImage(QRectF(0,0,40,40),this->image);
    pen.setColor(Qt::black);
    painter->setPen(pen);
    painter->drawText(QRectF(0,40,40,20),Qt::AlignCenter,QString::number(this->getCHID()));
    QRadialGradient Radial(20,20,this->radius,20,20);    //设置圆的原点和焦点在中心,半径120
    this->chColour.setAlpha(150);

    Radial.setColorAt(0,Qt::transparent);
    Radial.setColorAt(0.5,this->chColour.lighter(200));        //设置50%处的半径为蓝色
    Radial.setColorAt(1,this->chColour.darker(100));
    painter->setPen(Qt::transparent);
    painter->setBrush(Radial);
    drad=this->radius*((qreal)(this->flash+1)/this->range);
    painter->drawEllipse(-drad+20,-drad+20,drad*2,drad*2);
    if(this->isSelected())
    {
        pen.setColor(Qt::black);
        pen.setWidth(2);
        painter->setPen(pen);
        painter->setBrush(Qt::transparent);
        painter->drawRect(this->boundingRect());
    }
}

void GwDetector::setPropertyTree(QtTreePropertyBrowser *treepro)
{
    QtProperty *item;
    treepro->clear();
    foreach(item, m_propertys)
    {
        this->refreshProperty(item);
        treepro->addProperty(item);
    }
    treepro->setFactoryForManager(m_pVarManager,m_pVarFactory);
    treepro->setFactoryForManager(m_stringManager,m_stringFactory);
}

void GwDetector::refreshProperty(QtProperty *item)
{
    QPointF rec=this->mapToScene(this->m_rect.topLeft());
    if(item->propertyName()==QStringLiteral("X"))
        m_pVarManager->setValue(item,rec.x());
    else if(item->propertyName()==QStringLiteral("Y"))
        m_pVarManager->setValue(item,rec.y());
    else if(item->propertyName()==QStringLiteral("通道ID"))
        m_pVarManager->setValue(item,this->chID);
}

void GwDetector::refreshPropertyList()
{
    QtProperty *item;
    foreach (item, m_propertys) {
        this->refreshProperty(item);
    }
}


void GwDetector::createPropertyList()
{
    if(m_pVarManager!=NULL)
    {
        m_pVarManager->clear();
        delete m_pVarManager;
    }
    if(m_pVarFactory!=NULL)
    {
        delete m_pVarFactory;
    }
    this->m_pVarManager =new QtVariantPropertyManager();
    this->m_pVarFactory=new QtVariantEditorFactory();

    this->m_stringManager=new QtStringPropertyManager();
    this->m_stringFactory=new fileEditFactory();


    QtVariantProperty *item =m_pVarManager->addProperty(QVariant::Int, QStringLiteral("X"));
    m_pVarManager->setValue(item,m_rect.x());
    m_propertys.append(item);

    item =m_pVarManager->addProperty(QVariant::Int, QStringLiteral("Y"));
    m_pVarManager->setValue(item,m_rect.y());
    m_propertys.append(item);

    item =m_pVarManager->addProperty(QVariant::Int, QStringLiteral("通道ID"));
    m_pVarManager->setValue(item,this->chID);
    m_propertys.append(item);

    item = m_pVarManager->addProperty(QtVariantPropertyManager::enumTypeId(),
                    QStringLiteral("测试状态"));
    QStringList enumNames;
    enumNames << "正常" << "低报" << "高报";
    item->setAttribute(QStringLiteral("enumNames"), enumNames);
    item->setValue(0);
    m_propertys.append(item);

    connect(m_pVarManager,&QtVariantPropertyManager::valueChanged,this,&GwDetector::propertyChanged);
    connect(m_stringManager,&QtStringPropertyManager::valueChanged,this,&GwDetector::propertyChanged);
}

void GwDetector::setCHID(int ch)
{
    this->chID=ch;
}

void GwDetector::setConfig()
{
    this->setFlags(ItemIsSelectable | ItemIsMovable);
    this->setAcceptHoverEvents(true);
}

GraphicsState GwDetector::getState()
{
    return this->itemstate;
}

GwDetector &GwDetector::operator=(const GwDetector &other)
{
    this->setPos(other.pos());
    this->chID=other.chID;
    return *this;
}

QDataStream & operator >>(QDataStream &in, GwDetector &data)
{
    qreal x,y;
    int ch;
    in>>x>>y>>ch;
    QPointF cent=data.mapToScene(x,y);
    data.setPos(cent);
    data.chID=ch;
    return in;
}

QDataStream & operator <<(QDataStream &out, const GwDetector &data)
{
    QPointF rec=data.mapToScene(data.m_rect.topLeft());
    out
    <<rec.x()
    <<rec.y()
    <<data.chID;
    return out;
}

int GwDetector::getCHID()
{
    return this->chID;
}

void GwDetector::propertyChanged(QtProperty *property, const QVariant &val)
{
    QPointF cent=mapToScene(this->m_rect.topLeft());
    if(property->propertyName()==QStringLiteral("宽度")){
        this->m_rect.setWidth(val.toInt());
    }
    else if(property->propertyName()==QStringLiteral("高度")){
        this->m_rect.setHeight(val.toInt());
    }
    else if(property->propertyName()==QStringLiteral("X")){
        cent.setX(val.toInt());
        this->setPos(cent);
    }
    else if(property->propertyName()==QStringLiteral("Y")){
        cent.setY(val.toInt());
        this->setPos(cent);
    }
    else if(property->propertyName()==QStringLiteral("通道ID")){
        this->chID=val.toInt();
    }
    else if(property->propertyName()==QStringLiteral("测试状态")){
        qDebug()<<m_pVarManager->value(property).toString();
        switch(m_pVarManager->value(property).toInt())
        {
        case 0:
            this->setState(GState_OK);
            break;
        case 1:
            this->setState(GState_LO);
            break;
        case 2:
            this->setState(GState_HI);
            break;
        }
    }
    scene()->update();
}

void GwDetector::movieRun(int frame)
{
    this->flash=frame;
    scene()->update();
//    this->update();
}

void GwDetector::setState(GraphicsState state)
{
    this->itemstate=state;
    switch (state) {
    case GState_OK:
    {
        this->flash=0;
        this->chColour=Qt::green;
        this->movie->stop();
        break;
    }
    case GState_LO:
    {
        this->flash=0;
        this->chColour=Qt::yellow;
        this->movie->setDuration(1200);
        this->movie->start();
        break;
    }
    case GState_HI:
    {
        this->flash=0;
        this->chColour=Qt::red;
        this->movie->setDuration(600);
        this->movie->start();
        break;
    }
    case GState_DIS:
    {
        this->flash=0;
        this->chColour=Qt::gray;
        this->movie->stop();
        break;
    }
    }
    this->scene()->update();
}


void GwDetector::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    this->refreshPropertyList();
    QGraphicsItem::mouseMoveEvent(event);
}
